package com.rainbow.support.message.notify.dingtalk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.rainbow.support.message.notify.properties.MessageProperties;
import com.taobao.api.ApiException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 钉钉消息通知服务
 * @author 傅为地
 */
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DingTalkTemplate {

    private MessageProperties messageProperties;

    /**
     * 发送文本消息
     *
     * @param messageText 文本消息内容
     */
    public void sendMessage(String messageText) {
        try {
            DingTalkClient client = new DefaultDingTalkClient(messageProperties.getDing().getPatch() + "?access_token=" + messageProperties.getDing().getToken());
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent(messageText);
            request.setText(text);
            client.execute(request);
            log.info("发送钉钉通知成功:{}",messageText);
        } catch (ApiException e) {
            log.error("发送钉钉通知异常", e);
        }
    }
}
